package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.common.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.common.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category>page =  categoryMapper.pageQuery(categoryPageQueryDTO);
        long total = page.getTotal();
        List<Category> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
        category.setSort(1);
        //获取操作时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.innerSave(category);

    }

    @Override
    public void startOrStop(Integer status, Long id) {
        categoryMapper.startOrStop(status,id);
    }

    @Override
    public List<Category> list(Integer type) {
       List<Category> list = categoryMapper.list(type);
       return list;

    }

    @Override
    public void updateCategory(Category category) {
        //获取修改时间
        category.setUpdateTime(LocalDateTime.now());
        //执行修改
        categoryMapper.updateCategory(category);

    }

    @Override
    public void deleteCategory(Long id) {
        categoryMapper.deleteCategory(id);

    }

}
